/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-parts
*
* @File name : PartService.java
*
* @Author : zhangfm
*
* @Date : 2017年4月24日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年4月11日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.parts.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.marketing.common.dms.http.HttpCommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yonyou.gmmc.parts.contants.PartsConstant;
import com.yonyou.gmmc.parts.dao.TmCarPartsMapper;
import com.yonyou.gmmc.parts.dto.PartDto;
import com.yonyou.gmmc.parts.model.TmCarParts;

import tk.mybatis.mapper.entity.Example;

/**
 *
 * @author zhangfm
 * @date 2017年4月21日
 */
@Transactional
@Service("partService")
public class PartService {
	private static final Logger logger = Logger.getLogger(PartService.class);

	@Autowired
	private TmCarPartsMapper tmCarPartMapper;

	@Value("${DCMS-url}")
	private String dcmsUrl;

	/**
	 * 零部件列表查詢
	 * 
	 * @author zhangfm
	 * @date 2017年4月24日
	 * @return
	 */
	public PageInfo<PartDto> queryPartList(Map<String, Object> paramMap) {
		Map<String, Object> retMap = new HashMap<>();
		PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()),
				Integer.parseInt(paramMap.get("pageSize").toString()));
		List<PartDto> dtoList = tmCarPartMapper.queryPartList(paramMap);
		PageInfo<PartDto> page = new PageInfo<PartDto>(dtoList);
		// retMap.put("List", dtoList);
		if (dtoList.size() == 0) {
			retMap.put("flag", 0);
			retMap.put("msg", "查询不到数据");
		}
		return page;
	}

	/**
	 * 零部件按车系查詢
	 * 
	 * @author zhangfm
	 * @date 2017年4月21日
	 * @return
	 */
	public Map<String, Object> queryPartBySeries(@Param("carSeriesId") String carSeriesId) {
		Map<String, Object> retMap = new HashMap<>();
		List<PartDto> dtoList = tmCarPartMapper.queryPartBySeries(carSeriesId);
		retMap.put("List", dtoList);
		if (dtoList.size() == 0) {
			retMap.put("flag", 0);
			retMap.put("msg", "查询不到数据");
		}
		return retMap;
	}

	/**
	 * 查询品牌，零件种类 author zhangfm date 2017年5月19日
	 */
	public Map<String, Object> queryBrand() {
		Map<String, Object> retMap = new HashMap<>();
		List<PartDto> brandList = tmCarPartMapper.queryBrand();
		List<PartDto> typeList = tmCarPartMapper.queryType();
		retMap.put("brand", brandList);
		retMap.put("type", typeList);
		if (brandList.size() == 0 || typeList.size() == 0) {
			retMap.put("flag", 0);
			retMap.put("msg", "查询不到数据");
		}
		return retMap;
	}

	/**
	 * 按零件名或零件号查询
	 * 
	 * @author zhangfm
	 * @date 2017年5月19日
	 */
	public Map<String, Object> partByName(@Param("partName") String partName) {
		Map<String, Object> retMap = new HashMap<>();
		List<PartDto> dtoList = tmCarPartMapper.partByName(partName);
		retMap.put("List", dtoList);
		if (dtoList.size() == 0) {
			retMap.put("flag", 0);
			retMap.put("msg", "查询不到数据");
		}
		return retMap;
	}

	/**
	 * 按品牌和零件类型查找零部件
	 * 
	 * @author zhangfm
	 * @date 2017年5月19日
	 */
	public Map<String, Object> queryByIdType(@Param("brand") String brand, @Param("type") String type)
			 {
		Map<String, Object> retMap = new HashMap<>();
		List<PartDto> dtoList = tmCarPartMapper.queryByIdType(brand, type);
		retMap.put("List", dtoList);
		if (dtoList.size() == 0) {
			retMap.put("flag", 0);
			retMap.put("msg", "查询不到数据");
		}
		return retMap;
	}

	/**
	 * 将excel导入数据库
	 * 
	 * @author zhangfm
	 * @date 2017年4月27日
	 * @return
	 */
	public void importExcel(@Param("list") List<PartDto> listDto){
		tmCarPartMapper.importExcel(listDto);
	}

	/**
	 * 纯正零部件价格同步
	 * 
	 * @author Administrator
	 * @date 2017年6月7日
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> updatePartPrice() {
		Map<String, Object> result = new HashMap<>();
		try {
			// 查出零件的编码
			List<TmCarParts> list = tmCarPartMapper.selectAll();
			logger.info("一共有" + list.size() + "条零件数据");
			if (list.size() == 0) {
				logger.info("暂无零件信息");
			} else {
				List<Map<String, Object>> li = new ArrayList<>();
				for (int i = 0; i < list.size(); i++) {
					String code = list.get(i).getPartCode();
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("partCode", code);
					li.add(map);
					// strArray.add(pa);
				}
				logger.info("调用DCMS接口——Start");
				String uri = PartsConstant.PARTINFO_URL;
				List<TmCarParts> resultList = HttpCommonUtil.getOwnerInfos(li, "IC013", uri, TmCarParts.class);
				logger.info("调用DCMS接口——End");
				if (resultList.size() == 0) {
					logger.info("没有数据！");
				} else {
					// 同步零部件数据
					TmCarParts paramDto = new TmCarParts();
					for (int i = 0; i < resultList.size(); i++) {
						// 创建Example
						paramDto.setPartCode(resultList.get(i).getPartCode());
						paramDto.setPartPrice(resultList.get(i).getPartPrice());
						Example example = new Example(TmCarParts.class);
						example.createCriteria().andCondition("PART_CODE=", resultList.get(i).getPartCode());
						int a = tmCarPartMapper.updateByExampleSelective(paramDto, example);
						logger.info("更新数为>>>>>" + a);
					}
				}
			}
			result.put("errorCode", "0");
			result.put("errorMsg", "同步数据完成！");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.put("errorCode", "1");
			result.put("errorMsg", "同步数据错误！");
		}
		return result;
	}
}
